/**
 *  Copyright 2011 Miguel Ángel López Vicente 
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package cc.radio.ui;

import java.util.Vector;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import cc.radio.app.Programa;
import cc.radio.app.Radio;


public class ListaProgramas extends Activity {

	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listaprogramas);
        ((TextView) findViewById(R.id.titulo)).setText("Nuestros Programas");
        new Programas().execute();

    }
    
    public void click(View v) {
    	ProgramaUI e = (ProgramaUI) v.getParent();
    	Intent data = new Intent(v.getContext(), ListaCapitulos.class);
    	data.putExtra("programa", e.getPrograma());
    	startActivity(data);
    }
    
    

    
    private class Programas extends AsyncTask<Void, Void, Vector<Programa>> {
    	private ProgressDialog dialog;
    	
    	public void onPreExecute() {
    		dialog = new ProgressDialog(ListaProgramas.this);
    		dialog.setTitle("Descargando...");
    		dialog.setMessage("Obteniendo lista de programas...");
    		dialog.setCancelable(false);
    		dialog.show();
    	}

		protected Vector<Programa> doInBackground(Void... a) {
			
			return Radio.getProgramas();
		}
    	
		protected void onPostExecute(Vector<Programa> programas) {

			LinearLayout lista = (LinearLayout) findViewById(R.id.programas);			
	        for (Programa e : programas) 
	        	lista.addView(new ProgramaUI(ListaProgramas.this, e));

	        
	        dialog.dismiss();
	        
			if (programas.size() == 0) {
				Toast.makeText(getApplicationContext(), "Fallo de conexión.", Toast.LENGTH_SHORT).show();
				finish();
			}
		}
    	
    	
    }
}